package com.itany.crawler.proxy;

import com.itany.crawler.UrlBaseHandle;
import com.itany.crawler.Urls;
import org.apache.http.HttpHost;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.HashMap;
import java.util.Map;

public class Proxy89IP extends UrlBaseHandle {
    //    String url_proxy = "https://www.89ip.cn/index_1.html";
//    String url_proxy2 = "https://www.89ip.cn/index_2.html";
    String url_proxy = "https://www.89ip.cn/index_";
    public static Integer nextPage = 1;// 页数

    // 前三页pproxiIP
    static int maxPageNum = 3;
    public static Map<HttpHost, Integer> proxys = new HashMap<>();

    public Proxy89IP(boolean proxyed) {
        super(proxyed);
        setUrl(url_proxy + nextPage + ".html");
        nextPage %= maxPageNum;
        nextPage++;
    }

    @Override
    public String handle(Urls urls) throws Exception {
        String html = doGet();
        Document doc = Jsoup.parse(html, "UTF-8");

        Element element = doc.getElementsByClass("layui-table").get(0);

        element.getElementsByTag("tbody").get(0).getElementsByTag("tr").forEach(tr -> {
            String ip = tr.getElementsByTag("td").get(0).text();
            String portStr = tr.getElementsByTag("td").get(1).text();
            Integer port = Integer.valueOf(portStr);

            HashMap<HttpHost, Integer> host = new HashMap<>();
            HttpHost hh = new HttpHost(ip, port, "HTTP");
            proxys.put(hh, MyProxyManager.deafult_weight);
        });

//        System.out.println(element);
        return null;
    }


    public static void main(String[] args) throws Exception {
        Proxy89IP proxy89IP = new Proxy89IP(false);
        proxy89IP.handle(null);

        proxys.forEach((k, v) -> {
            System.out.println(k + "  " + v);
        });
    }
}
